import request from '@/utils/request'

// ==================== 商品分析统计（管理员） ====================

// 获取商品分析统计数据
export function getProductAnalytics(productId) {
  return request({
    url: productId ? `/api/v1/statistics/products/analytics/${productId}` : '/api/v1/statistics/products/analytics',
    method: 'get'
  })
}

// 获取商品转化率数据
export function getProductConversionRate(productId) {
  return request({
    url: `/api/v1/statistics/products/conversion/${productId}`,
    method: 'get'
  })
}

// 获取热门商品排行
export function getHotProductsRanking(limit = 10) {
  return request({
    url: '/api/v1/statistics/products/hot',
    method: 'get',
    params: { limit }
  })
}

// ==================== 用户行为分析（管理员） ====================

// 获取用户行为统计
export function getUserBehaviorStats(userId) {
  return request({
    url: userId ? `/api/v1/statistics/user-behavior/${userId}` : '/api/v1/statistics/user-behavior',
    method: 'get'
  })
}

// 获取用户行为历史
export function getUserBehaviorHistory(userId, params) {
  return request({
    url: `/api/v1/statistics/user-behavior/history/${userId}`,
    method: 'get',
    params
  })
}

// 获取用户分类偏好
export function getUserCategoryPreferences(userId, limit = 5) {
  return request({
    url: `/api/v1/statistics/user-behavior/preferences/${userId}`,
    method: 'get',
    params: { limit }
  })
}

// 获取用户行为趋势
export function getUserBehaviorTrend(userId, behaviorType, days) {
  return request({
    url: `/api/v1/statistics/user-behavior/trend/${userId}`,
    method: 'get',
    params: { behaviorType, days }
  })
}

// 获取用户行为热力图
export function getUserBehaviorHeatmap(userId, behaviorType) {
  return request({
    url: `/api/v1/statistics/user-behavior/heatmap/${userId}`,
    method: 'get',
    params: { behaviorType }
  })
}

// ==================== 评论统计分析（管理员） ====================

// 获取评论统计数据
export function getCommentStatistics(productId) {
  return request({
    url: productId ? `/api/v1/statistics/comments/statistics/${productId}` : '/api/v1/statistics/comments/statistics',
    method: 'get'
  })
}

// ==================== 商家销售统计（管理员） ====================

// 获取商家销售统计（管理员查看指定商家）
export function getMerchantSalesStatistics(userId) {
  return request({
    url: '/api/v1/statistics/merchant/sales',
    method: 'get',
    params: { userId }
  })
}

// ==================== 商家统计分析（商家） ====================

// 获取当前商家销售统计
export function getMyMerchantSalesStatistics() {
  return request({
    url: '/api/v1/statistics/merchant/sales/my',
    method: 'get'
  })
}

// 获取商家销售趋势
export function getMerchantSalesTrend(startTime, endTime, type = 'day') {
  return request({
    url: '/api/v1/statistics/merchant/sales/trend',
    method: 'get',
    params: {
      startTime,
      endTime,
      type
    }
  })
}

// 获取订单完成率统计
export function getOrderCompletionRate(startTime, endTime) {
  return request({
    url: '/api/v1/statistics/merchant/orders/completion',
    method: 'get',
    params: { startTime, endTime }
  })
}

// 获取订单完成率趋势
export function getOrderCompletionRateTrend(startTime, endTime) {
  return request({
    url: '/api/v1/statistics/merchant/orders/completion/trend',
    method: 'get',
    params: {
      startTime,
      endTime
    }
  })
}

// 获取商家商品数量统计
export function getMerchantProductCount() {
  return request({
    url: '/api/v1/statistics/merchant/products/count',
    method: 'get'
  })
}

// 获取商家今日订单概览
export function getMerchantTodayOrderStats() {
  return request({
    url: '/api/v1/statistics/merchant/orders/today',
    method: 'get'
  })
}

// 获取商家热销商品TOP5
export function getMerchantHotProducts() {
  return request({
    url: '/api/v1/statistics/merchant/products/hot',
    method: 'get'
  })
}

// ==================== 推荐系统分析（管理员） ====================

// 获取推荐效果统计
export function getRecommendationEffectiveness() {
  return request({
    url: '/api/v1/statistics/recommendation/effectiveness',
    method: 'get'
  })
}

// 获取实验分析数据
export function getExperimentAnalysis(experimentId) {
  return request({
    url: `/api/v1/statistics/recommendation/experiment/${experimentId}`,
    method: 'get'
  })
}

// 获取用户推荐行为数据
export function getUserRecommendationBehavior(userId) {
  return request({
    url: `/api/v1/statistics/recommendation/user-behavior/${userId}`,
    method: 'get'
  })
}

// ==================== 推荐系统管理（用户和管理员） ====================

// 获取个性化推荐商品
export function getPersonalizedRecommendations(userId, limit = 20) {
  return request({
    url: `/api/v1/product/recommendation/personalized/${userId}`,
    method: 'get',
    params: { limit }
  })
}

// 获取相似商品推荐
export function getSimilarProducts(productId, limit = 10) {
  return request({
    url: `/api/v1/product/recommendation/similar/${productId}`,
    method: 'get',
    params: { limit }
  })
}

// 获取热门商品推荐
export function getHotProducts(limit = 20) {
  return request({
    url: '/api/v1/product/recommendation/hot',
    method: 'get',
    params: { limit }
  })
}

// 获取新品推荐
export function getNewProducts(limit = 20) {
  return request({
    url: '/api/v1/product/recommendation/new',
    method: 'get',
    params: { limit }
  })
}

// 获取基于浏览历史的推荐
export function getBrowseHistoryRecommendations(userId, limit = 20) {
  return request({
    url: `/api/v1/product/recommendation/browse-history/${userId}`,
    method: 'get',
    params: { limit }
  })
}

// 获取基于收藏的推荐
export function getFavoriteRecommendations(userId, limit = 20) {
  return request({
    url: `/api/v1/product/recommendation/favorite/${userId}`,
    method: 'get',
    params: { limit }
  })
}

// ==================== 实验组管理（仅管理员） ====================

// 查询用户实验组信息
export function queryUserExperimentGroups(userIds) {
  return request({
    url: '/api/v1/product/recommendation/experiment/query',
    method: 'post',
    data: userIds
  })
}

// 重新分配用户实验组
export function reassignUserExperimentGroup(userId) {
  return request({
    url: `/api/v1/product/recommendation/experiment/reassign/${userId}`,
    method: 'post'
  })
}

// 批量重置用户实验组
export function reassignUsersToGroup(userIds, targetGroup) {
  return request({
    url: '/api/v1/product/recommendation/experiment/batch-reassign',
    method: 'post',
    data: userIds,
    params: { targetGroup }
  })
}

// 获取所有活跃的实验配置
export function getActiveExperiments() {
  return request({
    url: '/api/v1/product/recommendation/experiment/active',
    method: 'get'
  })
}

// 获取实验组用户统计
export function getExperimentGroupUserCounts() {
  return request({
    url: '/api/v1/product/recommendation/experiment/stats/user-counts',
    method: 'get'
  })
}

// 获取实验组效果指标统计
export function getExperimentGroupMetrics(startTime, endTime) {
  return request({
    url: '/api/v1/product/recommendation/experiment/stats/metrics',
    method: 'get',
    params: { startTime, endTime }
  })
}

// 获取用户推荐历史
export function getUserRecommendationHistory(userId, startTime, endTime) {
  return request({
    url: `/api/v1/product/recommendation/history/${userId}`,
    method: 'get',
    params: { startTime, endTime }
  })
}


